import React, { useState } from 'react'
import { Table, Button, Modal } from "antd";
import IconMap from "@/components/IconMap";
import AddModalCom from "../AddModalCom";
import { isArrNull } from '@/utils/ArrIncluenull';
const { Column } = Table;
export default function index({ childList = [], pushOrUpdateList, modalType }) {
    const [isShowDelModal, setIsShowDelModal] = useState(false);
    const [isShowAddModal, setIsShowAddModal] = useState(false);
    const [delIds, setDelIds] = useState([]);

    // 获取要删除子部门的id值
    const getDelChildIds = (e, ids) => {
        console.log(ids);
        setDelIds(ids);
    }

    // 删除调用
    const delDepartment = () => {
        if (modalType === "update") {
            pushOrUpdateList({
                type: "del",
                list: delIds,
                isDelete: true
            });

        } else {
            pushOrUpdateList({
                type: "add",
                list: delIds,
                isDelete: true
            });
        }
        setIsShowDelModal(false)

    }

    // 展示要添加的Modal
    const addChildList = () => {
        setIsShowAddModal(true);
    }

    return (
        <>
            <Table
                rowKey={el => el?._id}
                dataSource={isArrNull(childList) ? childList : []}
                rowSelection={{ onChange: getDelChildIds }}
                pagination={false}>
                <Column title="名称" dataIndex={"departmentName"}></Column>
            </Table>
            <Button type='primary' icon={IconMap.detail} onClick={addChildList}>增加子部门</Button>
            <Button icon={IconMap.del} disabled={false} onClick={() => setIsShowDelModal(true)}>解除子部门关联</Button>
            {/* 解除子部门关联Modal */}
            <Modal open={isShowDelModal} title={"提示"} onCancel={() => setIsShowDelModal(false)} onOk={delDepartment}>
                确定要删除选择的部门么?
            </Modal>

            <AddModalCom
                isShowAddModal={isShowAddModal}
                modalType={modalType}
                setIsShowAddModal={setIsShowAddModal}
                pushOrUpdateList={pushOrUpdateList}
            ></AddModalCom>
        </>
    )
}
